<?php
namespace Admin\Model;

use Think\Model;
use Think\Page;

class OrderModel extends Model{
    public function index(){
        
    }  
    public function Order_List($type=array(1)){
        
            $map['order_type']=array('in',$type);
            $page=$this->get_page($map);
            $result['show']=$page->show();
            $result['list']=$this->where($map)->limit($page->firstRow.','.$page->listRows)->select();
            return   $result;
    }
    
    public function Count_money($type=array(1)){
            $map['order_type']=array('in',$type);
            return $this->where($map)->sum('total_price');
    }
    
    public function Count_money_bySearch($map){
        return $this->where($map)->sum('total_price');
    }
    
    public function Edit_status($order_id){
        
            $map['order_id']=$order_id;
            return $this->where($map)->field('status,order_id')->find();
    }
    
    public function save_status($status){
            $map['order_id']=$status['order_id'];
            $data['status']=$status['status'];
            $data=$this->field('status')->create($data);
            $result=$this->where($map)->save();
            return $result>=0?true:false;
     }
     
     public function delete_order($info){
     
         $map['order_id']=$info['order_id'];
         $money=$this->where($map)->sum('total_price');
         $result=$this->where($map)->delete();
         $data['delCount']=$result;
         $data['money']=$money;
         return $data;
          
     }
     
     public function delete_all($info){
         $re= array();
         $re=explode('@',$info['ids']);
         if(!empty($re)){
             $map['order_id']=array('in',$re);
             $money=$this->where($map)->sum('total_price');
             $result=$this->where($map)->delete();
             $data['delCount']=$result;
             $data['money']=$money;
             return $data;
         }
         else{
             return false;
         }
         
     }
     
     public function query_order($data){
             $current_day=date('Y-m-d',time());
             $default_day=strtotime($current_day." 23:59:59");//默认的查询的截止日期为当天23:59:59
             $data['starttime']=($data['starttime']==''?strtotime('1970-01-01'):strtotime($data['starttime']));
             $data['endtime']=($data['endtime']==''?$default_day:strtotime($data['endtime']));
             $map['date']=array(array('EGT',$data['starttime']),array('ELT',$data['endtime']),'AND');
             $map['order_id']=array('like','%'.$data['order_id'].'%');
             $map['nick_name']=array('like','%'.$data['nick_name'].'%');
             $map['status']=array('like','%'.$data['status'].'%');
             $map['order_type']=array('like','%'.$data['order_type'].'%');
             $map['_logic']='and';
             $page=$this->get_page($map);
             $result['show']= $page->show();
             $result['list']=$this->where($map)->limit($page->firstRow.','.$page->listRows)->select();
             $result['money']=$this->Count_money_bySearch($map);
             return $result;
     }
     public function get_page($map){
         
         $count      = $this->where($map)->count();// 查询满足要求的总记录数
         $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(1)     
         $Page -> setConfig('prev','上一页');
         $Page -> setConfig('next','下一页');
         $Page->setConfig('theme', ' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
         return $Page;
         
     }
}